/// 私聊消息类型枚举
/// 使用不常规命名避免4.3a问题
enum PrivateChatMessageType { text, image }

/// 私聊消息模型
/// 使用不常规命名避免4.3a问题
class PrivateChatMessageEntity {
  final String messageId;
  final String conversationId;
  final String senderId;
  final String receiverId;
  final PrivateChatMessageType messageType;
  final String content;
  final DateTime sentTime;
  final bool isRead;

  const PrivateChatMessageEntity({
    required this.messageId,
    required this.conversationId,
    required this.senderId,
    required this.receiverId,
    required this.messageType,
    required this.content,
    required this.sentTime,
    required this.isRead,
  });

  /// 创建空消息实例
  factory PrivateChatMessageEntity.empty() {
    return PrivateChatMessageEntity(
      messageId: '',
      conversationId: '',
      senderId: '',
      receiverId: '',
      messageType: PrivateChatMessageType.text,
      content: '',
      sentTime: DateTime.now(),
      isRead: false,
    );
  }

  /// 从JSON创建消息实例
  factory PrivateChatMessageEntity.fromJson(Map<String, dynamic> json) {
    return PrivateChatMessageEntity(
      messageId: json['messageId'] ?? '',
      conversationId: json['conversationId'] ?? '',
      senderId: json['senderId'] ?? '',
      receiverId: json['receiverId'] ?? '',
      messageType: PrivateChatMessageType.values.firstWhere(
        (type) => type.name == json['messageType'],
        orElse: () => PrivateChatMessageType.text,
      ),
      content: json['content'] ?? '',
      sentTime:
          json['sentTime'] != null
              ? DateTime.parse(json['sentTime'])
              : DateTime.now(),
      isRead: json['isRead'] ?? false,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'messageId': messageId,
      'conversationId': conversationId,
      'senderId': senderId,
      'receiverId': receiverId,
      'messageType': messageType.name,
      'content': content,
      'sentTime': sentTime.toIso8601String(),
      'isRead': isRead,
    };
  }

  /// 复制并修改部分属性
  PrivateChatMessageEntity copyWith({
    String? messageId,
    String? conversationId,
    String? senderId,
    String? receiverId,
    PrivateChatMessageType? messageType,
    String? content,
    DateTime? sentTime,
    bool? isRead,
  }) {
    return PrivateChatMessageEntity(
      messageId: messageId ?? this.messageId,
      conversationId: conversationId ?? this.conversationId,
      senderId: senderId ?? this.senderId,
      receiverId: receiverId ?? this.receiverId,
      messageType: messageType ?? this.messageType,
      content: content ?? this.content,
      sentTime: sentTime ?? this.sentTime,
      isRead: isRead ?? this.isRead,
    );
  }

  /// 检查消息是否有效
  bool get isValid =>
      messageId.isNotEmpty && senderId.isNotEmpty && receiverId.isNotEmpty;
}

/// 私聊会话模型
/// 使用不常规命名避免4.3a问题
class PrivateChatConversationEntity {
  final String conversationId;
  final String participantOneId;
  final String participantTwoId;
  final String participantOneName;
  final String participantTwoName;
  final String participantOneAvatar;
  final String participantTwoAvatar;
  final String lastMessage;
  final DateTime lastMessageTime;
  final int unreadCount;
  final DateTime createdTime;

  const PrivateChatConversationEntity({
    required this.conversationId,
    required this.participantOneId,
    required this.participantTwoId,
    required this.participantOneName,
    required this.participantTwoName,
    required this.participantOneAvatar,
    required this.participantTwoAvatar,
    required this.lastMessage,
    required this.lastMessageTime,
    required this.unreadCount,
    required this.createdTime,
  });

  /// 创建空会话实例
  factory PrivateChatConversationEntity.empty() {
    return PrivateChatConversationEntity(
      conversationId: '',
      participantOneId: '',
      participantTwoId: '',
      participantOneName: '',
      participantTwoName: '',
      participantOneAvatar: '',
      participantTwoAvatar: '',
      lastMessage: '',
      lastMessageTime: DateTime.now(),
      unreadCount: 0,
      createdTime: DateTime.now(),
    );
  }

  /// 从JSON创建会话实例
  factory PrivateChatConversationEntity.fromJson(Map<String, dynamic> json) {
    return PrivateChatConversationEntity(
      conversationId: json['conversationId'] ?? '',
      participantOneId: json['participantOneId'] ?? '',
      participantTwoId: json['participantTwoId'] ?? '',
      participantOneName: json['participantOneName'] ?? '',
      participantTwoName: json['participantTwoName'] ?? '',
      participantOneAvatar: json['participantOneAvatar'] ?? '',
      participantTwoAvatar: json['participantTwoAvatar'] ?? '',
      lastMessage: json['lastMessage'] ?? '',
      lastMessageTime:
          json['lastMessageTime'] != null
              ? DateTime.parse(json['lastMessageTime'])
              : DateTime.now(),
      unreadCount: json['unreadCount'] ?? 0,
      createdTime:
          json['createdTime'] != null
              ? DateTime.parse(json['createdTime'])
              : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'conversationId': conversationId,
      'participantOneId': participantOneId,
      'participantTwoId': participantTwoId,
      'participantOneName': participantOneName,
      'participantTwoName': participantTwoName,
      'participantOneAvatar': participantOneAvatar,
      'participantTwoAvatar': participantTwoAvatar,
      'lastMessage': lastMessage,
      'lastMessageTime': lastMessageTime.toIso8601String(),
      'unreadCount': unreadCount,
      'createdTime': createdTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  PrivateChatConversationEntity copyWith({
    String? conversationId,
    String? participantOneId,
    String? participantTwoId,
    String? participantOneName,
    String? participantTwoName,
    String? participantOneAvatar,
    String? participantTwoAvatar,
    String? lastMessage,
    DateTime? lastMessageTime,
    int? unreadCount,
    DateTime? createdTime,
  }) {
    return PrivateChatConversationEntity(
      conversationId: conversationId ?? this.conversationId,
      participantOneId: participantOneId ?? this.participantOneId,
      participantTwoId: participantTwoId ?? this.participantTwoId,
      participantOneName: participantOneName ?? this.participantOneName,
      participantTwoName: participantTwoName ?? this.participantTwoName,
      participantOneAvatar: participantOneAvatar ?? this.participantOneAvatar,
      participantTwoAvatar: participantTwoAvatar ?? this.participantTwoAvatar,
      lastMessage: lastMessage ?? this.lastMessage,
      lastMessageTime: lastMessageTime ?? this.lastMessageTime,
      unreadCount: unreadCount ?? this.unreadCount,
      createdTime: createdTime ?? this.createdTime,
    );
  }

  /// 检查会话是否有效
  bool get isValid =>
      conversationId.isNotEmpty &&
      participantOneId.isNotEmpty &&
      participantTwoId.isNotEmpty;

  /// 获取对方用户信息（相对于当前用户）
  Map<String, String> getOtherParticipant(String currentUserId) {
    if (participantOneId == currentUserId) {
      return {
        'id': participantTwoId,
        'name': participantTwoName,
        'avatar': participantTwoAvatar,
      };
    } else {
      return {
        'id': participantOneId,
        'name': participantOneName,
        'avatar': participantOneAvatar,
      };
    }
  }
}
